package com.gis.wc;

import javax.xml.soap.Node;

/**
 * @author LnnuUser
 * @create 2021-09-06-下午7:12
 */
public class Test {
    public static void main(String[] args) {
        // [4,2,3] + [6,4,6]  =  [7,9]
        // 324  + 646 = 970
        NodeList l1 = new NodeList(7);
        NodeList l2 = new NodeList(5);

        Test test = new Test();
        NodeList result = test.computeFigure(l1, l2);
        while((result = result.next) != null) {
            System.out.println(result.val);
        }


    }
    private NodeList computeFigure(NodeList l1, NodeList l2){

        NodeList head = null, tail = null;
        int carry = 0;

        // 判断链表是否为空
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;

            if (head == null) {
                head = tail = new NodeList(sum % 10);
            }else {
                tail.next = new NodeList(sum % 10);
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new NodeList(carry);
        }
        return head;
    }

}

class NodeList {
    int val = 0;
    NodeList next = null;

    public NodeList(){}
    public NodeList(int val){
        this.val = val;
    }
    public NodeList(int val, NodeList l) {
        this.val = val;
        this.next = l;
    }
}
